import controlP5.*;


String[] filenames;
int[] tracks;
ListBox songsList;
ListBox trackList;
Slider generateCount;
Slider leaveOriginalCount;
Button runGeneration;
CheckBox checkbox;

class GUI {
  ControlP5 cp5;

  Button openFileButton;

  GUI() {
    cp5 = new ControlP5(FractalPlayer.this);

    openFileButton = cp5.addButton("OpenFile")
      .setPosition(30, 30)
        .setSize(70, 19);

    runGeneration = cp5.addButton("run")
      .setPosition(660, 60)
        .setSize(70, 19)
          .setVisible(false);

    songsList = cp5.addListBox("Songs")
      .setPosition(30, 80)
        .setSize(170, 160)
          .setItemHeight(20)
            .setBarHeight(20);

    songsList.captionLabel().style().marginTop = 5;
    songsList.valueLabel().style().marginTop = 3;

    trackList = cp5.addListBox("Tracks")
      .setPosition(230, 80)
        .setSize(160, 220)
          .setItemHeight(20)
            .setBarHeight(20);

    trackList.captionLabel().style().marginTop = 5;
    trackList.valueLabel().style().marginTop = 3;   

    addAvalableSongs();
  }
}

void setupSliders() {
  ui.cp5.remove(ui.cp5.getController("generate"));
  ui.cp5.remove(ui.cp5.getController("leave"));

  // add a vertical slider
  generateCount = ui.cp5.addSlider("generate")
    .setPosition(430, 60)
      .setSize(200, 20)
        .setRange(0, 200)
          .setValue(10);

  // reposition the Label for controller 'slider'
  generateCount.getValueLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  generateCount.getCaptionLabel().align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);

  // add a vertical slider
  leaveOriginalCount = ui.cp5.addSlider("leave")
    .setPosition(430, 100)
      .setSize(200, 20)
        .setRange(0, finder.stats.notes.size())
          .setValue(0);

  // reposition the Label for controller 'slider'
  leaveOriginalCount.getValueLabel().align(ControlP5.LEFT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
  leaveOriginalCount.getCaptionLabel().align(ControlP5.RIGHT, ControlP5.BOTTOM_OUTSIDE).setPaddingX(0);
}

public void OpenFile(int theValue) {
  selectInput("Select a MID file to process:", "fileSelected");
}

void generate(int number) {
  generateLen = number;
}

void leave(int number) {
  leaveLen = number;
}

void run(int theValue){
  score.stop();
  score.update();
  generateNotes(generateLen, leaveLen); 
}


void fileSelected(File selection) {
  try {
    midSong.loadSongFP(selection.getAbsolutePath());
    println();
  }
  catch(Exception e) {
    println("File selection canceled");
  }
}

void controlEvent(ControlEvent theEvent) {
  if (theEvent.isGroup()) {
    // an event from a group e.g. scrollList
    println(theEvent.group().value()+" from "+theEvent.group());
  }

  if (theEvent.isGroup() && theEvent.name().equals("Songs")) {
    int test = (int)theEvent.group().value();
    midSong.loadSong(filenames[test]);
    currentTrackName = filenames[test];
  }

  if (theEvent.isGroup() && theEvent.name().equals("Tracks")) {
    int test = (int)theEvent.group().value();
    runGeneration.setVisible(true);
    calculateStats(test);
  }
}

void addAvalableSongs() {
  java.io.File folder = new java.io.File(dataPath("")+"/songs/");

  java.io.FilenameFilter midFilter = new java.io.FilenameFilter() {
    public boolean accept(File dir, String name) {
      return name.toLowerCase().endsWith(".mid");
    }
  };

  filenames = folder.list(midFilter);

  for (int i = 0; i < filenames.length; i++) {
    ListBoxItem lbi = songsList.addItem(filenames[i], i);
  }
}

